package com.be.beadmin.device.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.device.dto.output.EnergyInfoExcelEx;
import com.be.beadmin.device.entity.EnergyInfo;
import com.be.beadmin.device.dto.output.EnergyInfoEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import com.be.beadmin.device.service.EnergyInfoService;
import com.be.beadmin.device.dto.input.EnergyInfoQueryCriteria;
import com.be.beadmin.device.dto.input.EnergyInfoDto;
import org.springframework.data.domain.Pageable;
import com.be.beadmin.common.utils.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

/**
* @author chenxu
* @date 2023-08-30
**/
@Api(tags = {"能效"})
@RestController
public class EnergyInfoController {

    @Autowired
    private  EnergyInfoService energyInfoService;


    @PostMapping(value = "/device/getEnergyInfoByPage")
    public ResultModel getEnergyInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(energyInfoService.getEnergyInfoByPage(pageEntity)) ;
    }

    @PostMapping("/device/addEnergyInfo")
    public ResultModel addEnergyInfo(@RequestBody EnergyInfoDto energyInfoDto){
        energyInfoService.addEnergyInfo(energyInfoDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/device/delEnergyInfo")
    public ResultModel delEnergyInfo(@RequestBody EnergyInfoDto energyInfoDto){
        energyInfoService.delEnergyInfo(energyInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/device/updateEnergyInfo")
    public ResultModel updateEnergyInfo(@RequestBody EnergyInfoDto energyInfoDto){
        energyInfoService.updateEnergyInfo(energyInfoDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/device/exportEnergyInfo")
    public void exportEnergyInfo(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<EnergyInfoEx> energyInfoPage = energyInfoService.getEnergyInfoByPage(pageEntity);
        List<EnergyInfoEx> energyInfoExes = energyInfoPage.getContent();
        List<EnergyInfoExcelEx> energyInfoExcelExes=energyInfoExes.stream().map(item->{
            EnergyInfoExcelEx excel=new EnergyInfoExcelEx();
            BeanUtils.copyProperties(item,excel);
            return excel;
        }).collect(Collectors.toList());
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("EnergyInfo接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
        //EasyExcel.write(response.getOutputStream(), EnergyInfoExcelEx.class).sheet("sheet1").doWrite(energyInfoExcelExes);
        ExcelWriterBuilder ewb=EasyExcel.write(response.getOutputStream(), EnergyInfoExcelEx.class);
        ewb.excelType(ExcelTypeEnum.XLS);
        ewb.sheet("sheet1").doWrite(energyInfoExcelExes);
    }

    @ApiOperation("上传水电气能效文件")
    @PostMapping("/design/uploadEnergyInfo")
    public ResultModel uploadStandProductProcessInfo(@RequestParam("file") MultipartFile[] files) {
        for(MultipartFile file:files) {
            if (file.isEmpty()) {
                throw new BeadminException("请上传文件!");
            }
//            standProductProcessInfoService.uploadStandProductProcessInfo(file);
            energyInfoService.uploadEnergyInfo(file);
        }
        return ResultModel.success("上传成功");
    }


    @ApiOperation("获取所有设备")
    @PostMapping("/device/getAllDevice")
    public ResultModel getAllDevice() {
        String[] devices = energyInfoService.getAllDevice();
        return ResultModel.success(devices);
    }
}